﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GtsServer.Service.Model;
using GtsServer.Service.Model.Response;
using GtsServer.Model;
using GtsServer.DAL;
using System.Data;
using GtsServer.Common;

namespace GtsServer.Service.Controller
{
    public class ChanceController 
    {
        public ChanceController() { }

        public ChanceResponse GetChance()
        {
            ChanceResponse response = new ChanceResponse();
            response.model = new List<ChanceModelManage>();
            response.little_pic = new List<List<int>>();
            response.lucky_pic = new List<List<int>>();
            response.start = new List<List<int>>();
            response.end = new List<List<int>>();
            response.sel_high = new List<SelHighManage>();
            response.sel_low = new List<SelLowManage>();
            response.lucky_light = new List<LuckyLightManage>();
            response.cat = new List<CatManage>();
            response.sea = new List<List<int>>();
            response.bar = new List<List<int>>();
            response.plane = new List<CatManage>();

            //获取盈亏设置
            PLSettingDAL plSettingDal = new PLSettingDAL();
            DataSet ds = plSettingDal.GetList(string.Empty);
            if (ds.Tables[0].Rows.Count > 0)
            {
                PLSetting plSettionModel = IListDataSet.DataSetToIList<PLSetting>(ds, 0).First();
                response.gains = new PLSettingManage()
                {
                    first = plSettionModel.InitValue,
                    more = plSettionModel.OvertopValue,
                    less = plSettionModel.UnderValue,
                    normal = plSettionModel.NormValue
                };
            }
            //获取概率

            //获取概率配置（标准，高概率，低概率）
            ChanceTypeDAL chanceTypeDal = new ChanceTypeDAL();
            ds.Clear();
            ds = chanceTypeDal.GetList(string.Empty);
            List<ChanceType> chanceTypeList = IListDataSet.DataSetToIList<ChanceType>(ds, 0).ToList();
            Dictionary<int, ChanceType> dictChanceType = new Dictionary<int, ChanceType>();
            foreach (var item in chanceTypeList)
                dictChanceType[item.ID] = item;

            //获取概率值
            ChanceDAL chanceDal = new ChanceDAL();
            ds.Clear();
            ds = chanceDal.GetList(string.Empty);
            List<ChanceShow> chanceList = IListDataSet.DataSetToIList<ChanceShow>(ds, 0).ToList();

            Dictionary<string, Dictionary<int, List<Chance>>> dict = new Dictionary<string, Dictionary<int, List<Chance>>>();
            
            foreach (var item in chanceList)
            {
                if (!dict.ContainsKey(item.TypeName))
                    dict[item.TypeName] = new Dictionary<int, List<Chance>>();
                if (!dict[item.TypeName].ContainsKey(item.TypeID))
                    dict[item.TypeName][item.TypeID] = new List<Chance>();

                dict[item.TypeName][item.TypeID].Add(item);
            }

            //获取事件概率值
            Dictionary<string, Dictionary<int, EventControllerModel>> dictEvent = new Dictionary<string, Dictionary<int, EventControllerModel>>();
            EventDAL eventDal = new EventDAL();
            ds.Clear();
            ds = eventDal.GetEventAndMouldList();
            if (ds != null)
            {
                foreach (DataRow item in ds.Tables[0].Rows)
                {
                    string typeName = item["etypename"].ToString();
                    int typeID = Convert.ToInt32(item["ChanceTypeID"].ToString());
                    int chanceNum = Convert.ToInt32(item["ChangeNum"].ToString());
                    int chanceValue = Convert.ToInt32(item["ChanceValue"].ToString());

                    if (!dictEvent.ContainsKey(typeName))
                        dictEvent[typeName] = new Dictionary<int, EventControllerModel>();
                    if (!dictEvent[typeName].ContainsKey(typeID))
                    {
                        dictEvent[typeName][typeID] = new EventControllerModel();
                    
                        EventControllerModel model = new EventControllerModel()
                        {
                            changeValue = chanceNum,
                            value = new List<int>()
                        };
                        dictEvent[typeName][typeID] = model;
                    }
                    dictEvent[typeName][typeID].value.Add(chanceValue);
                }
            }

            foreach (var item in dict)
            {
                switch (item.Key.ToLower())
                {
                    case "model":
                        foreach (var chancemouldList in dict["model"])
                        {
                            ChanceModelManage cmModel = new ChanceModelManage()
                            {
                                max = new List<int>(),
                                min = new List<int>(),
                                total = 0
                            };
                            foreach (var chancemouldModel in chancemouldList.Value)
                            {
                                cmModel.max.Add(chancemouldModel.MaxNum);
                                cmModel.min.Add(chancemouldModel.MinNum);
                            }
                            cmModel.total = dictChanceType.ContainsKey(chancemouldList.Key) ? dictChanceType[chancemouldList.Key].HandCount : 0;
                            response.model.Add(cmModel);
                        }
                        break;
                    default:
                        break;
                }
            }
            foreach (var item in dictEvent)
            {
                switch (item.Key.ToLower())
                {
                    case "little_pic":
                        response.little_pic = (GetListInt(dictEvent["little_pic"]));
                        break;
                    case "lucky_pic":
                        response.lucky_pic = (GetListInt(dictEvent["lucky_pic"]));
                        break;
                    case "start":
                        response.start = (GetListInt(dictEvent["start"]));
                        break;
                    case "end":
                        response.end = (GetListInt(dictEvent["end"]));
                        break;

                    case "sel_high":
                        foreach (var selhighList in dictEvent["sel_high"])
                        {
                            SelHighManage shModel = new SelHighManage()
                            {
                                high = selhighList.Value.value,
                                low = selhighList.Value.changeValue
                            };
                            response.sel_high.Add(shModel);
                        }
                        break;
                    case "sel_low":
                        foreach (var selhighList in dictEvent["sel_low"])
                        {
                            SelLowManage shModel = new SelLowManage()
                            {
                                high = selhighList.Value.changeValue,
                                low = selhighList.Value.value
                            };
                            response.sel_low.Add(shModel);
                        }
                        break;
                    case "lucky_light":
                        foreach (var selhighList in dictEvent["lucky_light"])
                        {
                            LuckyLightManage shModel = new LuckyLightManage()
                            {
                                type = selhighList.Value.value,
                                low = selhighList.Value.changeValue
                            };
                            response.lucky_light.Add(shModel);
                        }
                        break;
                    case "cat":
                        foreach (var selhighList in dictEvent["cat"])
                        {
                            CatManage shModel = new CatManage()
                            {
                                fruit = selhighList.Value.value,
                                ratio = selhighList.Value.changeValue
                            };
                            response.cat.Add(shModel);
                        }
                        break;
                    case "sea":
                        response.sea = (GetListInt(dictEvent["sea"]));
                        break;
                    case "bar":
                        response.bar = (GetListInt(dictEvent["bar"]));
                        break;
                    case "plane":
                        foreach (var selhighList in dictEvent["plane"])
                        {
                            CatManage shModel = new CatManage()
                            {
                                fruit = selhighList.Value.value,
                                ratio = selhighList.Value.changeValue
                            };
                            response.plane.Add(shModel);
                        }
                        break;
                    default:
                        break;
                }
            }

            return response;
        }

        public List<List<int>> GetListInt(Dictionary<int, EventControllerModel> dict)
        {
            List<List<int>> list = new List<List<int>>();
            foreach (var chancelittlePicList in dict)
            {
                List<int> valueInt = chancelittlePicList.Value.value;

                list.Add(valueInt);
            }
            return list;
        }

        public List<List<int>> GetListInt(Dictionary<int, List<Chance>> dict)
        {
            List<List<int>> list = new List<List<int>>();
            foreach (var chancelittlePicList in dict)
            {
                List<int> valueInt = new List<int>();
                foreach (var chancelittlePicModel in chancelittlePicList.Value)
                    valueInt.Add(chancelittlePicModel.ChanceValue);
                list.Add(valueInt);
            }
            return list;
        }
    }

    public class EventControllerModel
    {
        public List<int> value;
        public int changeValue;
    }
}
